package com.hzz.service.examine;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.examine.IExamineTargetDetailService;
import com.hzz.dao.examine.IExamineTargetDetailDao;
import com.hzz.dto.examine.ExamineTargetDetailDto;
import com.hzz.dto.examine.ExamineTargetDto;
import com.hzz.entity.examine.ExamineTargetDetail;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.IdUtil;

import lombok.extern.slf4j.Slf4j;

@FeignService
@Slf4j
public class ExamineTargetDetailService extends BaseService implements IExamineTargetDetailService {
	@Autowired
	IExamineTargetDetailDao examineTargetDetailDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {
			Exception.class, RuntimeException.class })
	public Response saveOrUpdateData(ExamineTargetDetailDto dto) throws Exception {
		Response result = new Response(0, "success");
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			// 判断数据是否存在
			if (examineTargetDetailDao.isDataExist(entity) != 0) {
				// 数据存在
				examineTargetDetailDao.update(entity);
			} else {
				// 新增
				if (entity.getId() == null) {
					entity.setId(IdUtil.nextId());
				}
				examineTargetDetailDao.insert(entity);
				result.data = entity.getId();
			}
		} catch (Exception e) {
			log.error("信息保存异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public String deleteData(ExamineTargetDetailDto dto) throws Exception {
		String result = "success";
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			if (examineTargetDetailDao.deleteByPrimaryKey(entity) == 0) {
				throw new RuntimeException("数据不存在!");
			}
		} catch (Exception e) {
			log.error("物理删除异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {
			Exception.class, RuntimeException.class })
	public String deleteDataById(ExamineTargetDetailDto dto) throws Exception {
		String result = "success";
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			if (examineTargetDetailDao.deleteById(entity) == 0) {
				throw new RuntimeException("数据不存在!");
			}
		} catch (Exception e) {
			log.error("逻辑删除异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public PageInfo findDataIsPage(ExamineTargetDetailDto dto) throws Exception {
		PageInfo pageInfo = null;
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
			List list = examineTargetDetailDao.findDataIsPage(entity);
			pageInfo = new PageInfo(list);
			pageInfo.setList(copyTo(pageInfo.getList(), ExamineTargetDetailDto.class));
		} catch (Exception e) {
			log.error("信息[分页]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return pageInfo;
	}

	@Override
	public List<ExamineTargetDetailDto> findDataIsList(ExamineTargetDetailDto dto) throws Exception {
		List<ExamineTargetDetailDto> results = null;
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			results = copyTo(examineTargetDetailDao.findDataIsList(entity), ExamineTargetDetailDto.class);
		} catch (Exception e) {
			log.error("信息[列表]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return results;
	}

	@Override
	public ExamineTargetDetailDto findDataById(ExamineTargetDetailDto dto) throws Exception {
		ExamineTargetDetailDto result = null;
		try {
			ExamineTargetDetail entity = copyTo(dto, ExamineTargetDetail.class);
			result = copyTo(examineTargetDetailDao.selectByPrimaryKey(entity), ExamineTargetDetailDto.class);
		} catch (Exception e) {
			log.error("信息[详情]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public Response importTarget(ExamineTargetDto dto) throws Exception {
		Response result = new Response(0, "success");
		ExamineTargetDetailDto record = new ExamineTargetDetailDto();
		record.setTargetId(dto.getId());
		List<ExamineTargetDetailDto> datas = this.findDataIsList(record);
		if (datas != null) {
			for(ExamineTargetDetailDto detail : datas){
				//清除原数据信息
				detail.setId(null);
				detail.setTargetId(dto.getCurrTargetId());
				detail.setCreateId(dto.getCreateId());
				detail.setDateCreated(null);
				result = this.saveOrUpdateData(detail);
			}
		}
		return result;
	}

}
